// auth.js 应该这样实现
import http from './http.js' // 改为默认导入
const TOKEN_KEY = 'token'
const REFRESH_TOKEN_KEY = 'refresh_token'

export default {
  getToken() {
    return uni.getStorageSync(TOKEN_KEY)
  },

  setToken(token) {
    uni.setStorageSync(TOKEN_KEY, token)
  },

  getRefreshToken() {
    return uni.getStorageSync(REFRESH_TOKEN_KEY)
  },

  setRefreshToken(token) {
    uni.setStorageSync(REFRESH_TOKEN_KEY, token)
  },

  clearTokens() {
    uni.removeStorageSync(TOKEN_KEY)
    uni.removeStorageSync(REFRESH_TOKEN_KEY)
  },

  async login(phone, password) {
    try {
      const res = await http.post('/users/login', { phone, password });
      
      if (res) {
        this.setToken(res.data.token);
        this.setRefreshToken(res.data.refresh_token);
        return res.data.user_info;
      }
      throw new Error(res.message || '登录失败');
    } catch (e) {
      throw new Error(e.message || '网络请求异常');
    }
  },

  async valid() {
    try {
      // 现在会使用正确的http实例
      await http.get('/users/valid');
      return true;
    } catch (e) {
      this.clearTokens();
      return false;
    }
  },
    /**
   * 用户注销
   * @returns {Promise} 注销结果Promise
   */
    async logout() {
      return http.post('/users/logout')
    },
}